#include "gamehall.h"
#include "ui_gamehall.h"

#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QMessageBox>
#include <QByteArray>

GameHall::GameHall(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::GameHall)
{
    this->setWindowTitle("游戏大厅界面");
    ui->setupUi(this);
    connect(this,&GameHall::signal_CreateSeatSuccess,this,&GameHall::CreateSeatSuccess);
    connect(this,&GameHall::signal_CreateSeatFail,   this,&GameHall::CreateSeatFail);

    qRegisterMetaType<std::vector< std::pair<string,string> >>("std::vector<std::pair<string,string>>");
    connect(this,&GameHall::signal_FlashHome,        this,&GameHall::flashHome);

    connect(this,&GameHall::signal_JoinSuccess,       this,&GameHall::joinSuccess);
    connect(this,&GameHall::signal_JoinFail,          this,&GameHall::joinFail);
}

void GameHall::CreateSeatSuccess()
{
    QMessageBox::about(this,"创建房间成功","创建房间成功");
}

void GameHall::CreateSeatFail()
{
    QMessageBox::about(this,"创建房间失败","创建房间失败");
}

void GameHall::getDetailHomeInfo(QByteArray byte)
{

    ui->hall_list_widget->clear();
    QJsonParseError jsonError;
    QJsonDocument paserDoc = QJsonDocument::fromJson(byte,&jsonError);
    vector<pair<string,string>> ret;
    if (jsonError.error == QJsonParseError::NoError)
    {
        QJsonObject paserObj = paserDoc.object();
        QJsonArray arry= paserObj.find("seatinfo").value().toArray();
        for(int index=0;index<arry.size();index++)
        {
            ret.push_back(std::pair<string,string>(
                          arry.at(index).toObject()["home_name"].toString().toStdString(),
                          arry.at(index).toObject()["home_desp"].toString().toStdString()));
        }
    }

    emit signal_FlashHome(ret);
}

void GameHall::flashHome(std::vector<std::pair<string,string>> vec)
{
    for(auto it:vec)
    {
        ui->hall_list_widget->addItem(new QListWidgetItem((it.first.c_str()+QString("----")
                                                           +it.second.c_str())));
    }
}

GameHall::~GameHall()
{
    delete ui;
}

void GameHall::setSocket(shared_ptr<SockClient> msocket)
{
    m_cli=msocket;
    m_psw_wid.setSocket(m_cli);
}

void GameHall::on_hall_list_widget_itemDoubleClicked(QListWidgetItem *item)
{
    if(item==nullptr)
    {
        return;
    }
    m_psw_wid.setStatus(SetPasswordWid::JoinHome);
    auto st_list=item->text().split("----");

    m_psw_wid.initHomeInfo(st_list.front());
    m_psw_wid.show();
}

void GameHall::joinFail(QByteArray info)
{
    QJsonParseError jsonError;
    QJsonDocument paserDoc = QJsonDocument::fromJson(info,&jsonError);
    vector<pair<string,string>> ret;
    if (jsonError.error == QJsonParseError::NoError)
    {
        QJsonObject paserObj = paserDoc.object();
        QString fail_join_info= paserObj.find("reason").value().toString();
        QMessageBox::about(nullptr,"加入失败的原因",fail_join_info);
    }
}

void GameHall::on_create_home_butt_clicked()
{
    m_psw_wid.setStatus(SetPasswordWid::CreateHome);
    m_psw_wid.show();
}

void GameHall::on_join_home_butt_clicked()
{
    m_psw_wid.setStatus(SetPasswordWid::JoinHome);
    m_psw_wid.show();
}

void GameHall::on_flash_butt_clicked()
{
    QJsonObject json;
    json.insert("type",QString("Hall_GetDetailSeat"));
    QJsonDocument document;
    document.setObject(json);
    QByteArray bytearray = document.toJson(QJsonDocument::Compact);
    m_cli->writeQString(bytearray);
}

void GameHall::joinSuccess()
{
    QMessageBox::about(this,"加入房间成功","加入房间成功");
    emit signal_NetGameStart();
}


